package org.mxp.demo7;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import org.mxp.utils.RabbitMQUtils;

import java.util.Date;

public class ReceiveObj {
    public static void main(String[] args) throws Exception {
        // 交换机名称
        String EXCHANGE_NAME = "direct_logs";
        // 获取信道
        Channel channel = RabbitMQUtils.getChannel();
        // 声明交换机，第一个参数为交换机名称，第二个参数为交换机类型
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        // 声明console队列，并返回队列名称，返回值就是console
        String queueName1 = channel.queueDeclare("console", false, false, false, null).getQueue();
        // 绑定console队列与交换机，bindingkey（路由键）分别为info和warning
        channel.queueBind(queueName1, EXCHANGE_NAME, "info");
        channel.queueBind(queueName1, EXCHANGE_NAME, "warning");
        // 接受消息
        channel.basicConsume(queueName1, true, (consumerTag, message) -> {
            System.out.println(queueName1 + "接受到消息：" + new String(message.getBody()));
        }, (CancelCallback) null);

        // 声明disk队列，并返回队列名称，返回值就是disk
        String queueName2 = channel.queueDeclare("disk", false, false, false, null).getQueue();
        // 绑定disk队列与交换机，bindingkey（路由键）为error
        channel.queueBind(queueName2, EXCHANGE_NAME, "error");
        // 接受消息
        channel.basicConsume(queueName2, true, (consumerTag, message) -> {
            System.out.println(queueName2 + "接受到消息：" + new String(message.getBody()));
        }, (CancelCallback) null);
    }
}
